package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.ResponseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.mvp.presenter.LeavePresenter;
import com.android.infantschool.model.mvp.presenter.LeavePresenterImpl;
import com.android.infantschool.model.mvp.view.LeaveView;
import com.android.infantschool.util.DateUtil;
import com.android.infantschool.util.ToastUtil;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\5 0005 15:52
 * @class describe
 */
public class LeaveActivity extends BaseActivity implements LeaveView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.ed_content)
    public EditText edContent;
    @BindView(R.id.tv_content_size)
    public TextView tvContentSize;
    @BindView(R.id.tv_start_time)
    public TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    public TextView tvEndTime;
    @BindView(R.id.tv_approver)
    public TextView tvApprover;
    @BindView(R.id.btn_submit)
    public Button btnSubmit;
    @BindView(R.id.iv_approver)
    public ImageView ivApprover;

    private AlertDialog alertDialog = null;
    private LeavePresenter presenter = null;

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, LeaveActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        presenter = new LeavePresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("请假");
        tvStartTime.setText(DateUtil.getDate(DateUtil.ymdhms));
        tvEndTime.setText(DateUtil.getDate(DateUtil.ymdhms));
        edContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = s.toString().length();
                length = 500 - length;
                tvContentSize.setText(length + "/500");
            }
        });
    }

    @OnClick({R.id.btn_submit, R.id.img_back, R.id.tv_start_time, R.id.tv_end_time, R.id.tv_approver})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                String content = edContent.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtil.showShortToast(this, "请输入请假理由");
                    return;
                }
                this.showLoading();
                Map<String, String> map = new HashMap<>();
                map.put("token", AppConfig.getToken());//token
                map.put("createTime", tvStartTime.getText().toString());//请假开始时间
                map.put("endTime", tvEndTime.getText().toString());//请假结束时间
                map.put("cause", content);//请假理由
                map.put("approver", "园长");
                Log.e(TAG, "map:" + map.toString());
                presenter.getLeave(map);
                break;
            case R.id.img_back:
                this.finish();
                break;
            case R.id.tv_start_time:
                showYearMonthPicker(tvStartTime);
                break;
            case R.id.tv_end_time:
                showYearMonthPicker(tvEndTime);
                break;
            case R.id.tv_approver:
                showList(tvApprover);
                break;
        }
    }

    @Override
    public void errorLeave(String error) {
        this.dismissLoading();
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successLeave(BaseBean baseBean) {
        this.dismissLoading();
        ResponseBean responseBean = (ResponseBean) baseBean;
        if (responseBean.getCode() == 0) {
            ToastUtil.showShortToast(this, "提交成功");
            this.finish();
        } else {
            ToastUtil.showShortToast(this, "提交失败:" + responseBean.getMsg());
        }
    }

    public void showList(View view) {
        final String[] items = {"王老师", "刘主任", "院长", "张老师", "李老师"};
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
        alertBuilder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                tvApprover.setText(items[i]);
                alertDialog.dismiss();
            }
        });
        alertDialog = alertBuilder.create();
        alertDialog.show();
    }

    /**
     * 年月日选择
     */
    private void showYearMonthPicker(final TextView textView) {
        new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat formatter = new SimpleDateFormat(DateUtil.ymdhms);
                String dateString = formatter.format(date);
                textView.setText(dateString);
            }
        }).setType(new boolean[]{true, true, true, true, true, true}).setSubmitColor(0xFF007C70)
                .setCancelColor(0xFF999999)
                .build().show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.onDestroy();
            presenter = null;
        }
    }
}